package com.zzx.boot.config;

import com.zzx.boot.gilter.JwtFilter;
import com.zzx.boot.handler.MyAccessDeniedHandler;
import com.zzx.boot.handler.JwtAuthHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;

import javax.annotation.Resource;

/**
 * @author tan
 * @version 1.0
 * @date 2023/7/8 21:52
 */
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)//开启方法级别认证
public class SecurityConfig {

    @Resource
    private JwtFilter jwtFilter;
    @Resource
    private JwtAuthHandler jwtAuthHandler;
    @Resource
    private MyAccessDeniedHandler myAccessDeniedHandler;

    /**
     * 密码明文配置
     * @return
     */
    @Bean
    public PasswordEncoder getPasswordEncoder(){
        return new BCryptPasswordEncoder();
    }




    /**
     * 获取身份认证管理器
     * @param conf
     * @return
     */
    @Bean("authenticationManager")
    public AuthenticationManager getAuthenticationManager(AuthenticationConfiguration conf) throws Exception {
           return conf.getAuthenticationManager();
   }

    /**
     * 配置跨源访问(CORS)
     * @return
     */
    @Bean
    CorsConfigurationSource corsConfigurationSource() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", new CorsConfiguration().applyPermitDefaultValues());
        return source;
    }


    @Bean
    public SecurityFilterChain security(HttpSecurity httpSecurity) throws Exception {
        return httpSecurity.
                //基于token 不需要csrf
                csrf().disable()
                // 基于token 不需要session
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                // 认证异常
                .exceptionHandling().authenticationEntryPoint(jwtAuthHandler)
                // 权限异常
                .accessDeniedHandler(myAccessDeniedHandler)
                .and()
                // 权限
                .authorizeRequests()
                // 放行所有权限
                .antMatchers("/swagger-ui.html"
                        , "/webjars/**"
                        , "/v2/**"
                        , "/swagger-resources/**").permitAll()
                .antMatchers("/user/*/code",
                        "/user/bind/login",
                        "/user/login",
                        "/user/phone/login",
                        "/user/search",
                        "/user/bind/phone").permitAll()
                .antMatchers("/post/class",
                        "/post/get/*/post",
                        "/post/topic/*/post/*",
                        "/post/postclass/*/post/*",
                        "/post/user/*/post/*",
                        "/post/search/*/page").permitAll()
                .antMatchers("/topic/class",
                        "/topic/hot/topic",
                        "/topic/topicclass/*/topic/*",
                        "/topic/search/page/*").permitAll()
                .antMatchers("/adsense/*").permitAll()
                // 其他权限
                .anyRequest().authenticated()
                .and()
                // 过滤器
                .addFilterBefore(jwtFilter, UsernamePasswordAuthenticationFilter.class)
//                .userDetailsService(UserDetailsServiceImpl.class)
                .build();
    }



}
